/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAL.FlightsBean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mr-aUgust
 */
@WebServlet(name = "ControllerFlight", urlPatterns = {"/ControllerFlight"})
public class ControllerFlight extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        FlightsBean flbean=new FlightsBean();
        HttpSession session=request.getSession();
        if(request.getParameter("create")!=null)
        {
            if(request.getParameter("create").equals("Add New Flight"))
            {
                int namecity=Integer.parseInt(request.getParameter("namecity"));
                String flname=request.getParameter("flightname");
                float flpice=Float.parseFloat(request.getParameter("price"));
                String departuretime =request.getParameter("departurTime");
                String timedeparture=request.getParameter("timedeparture");
                String arrivaltime=request.getParameter("arrivalTime");
                String timearrival=request.getParameter("timearrival");
                String departureCity=request.getParameter("departureCity");
                String arrivalcity=request.getParameter("arrivalCity");
                if(flbean.InsertFlight(namecity,flname,departuretime+" "+timedeparture,arrivaltime+" "+timearrival,departureCity,arrivalcity,flpice)==true)
                {
                     out.println("<script>alert('Create flight Success!');window.location='Admin/management.jsp?page=Flights';</script>");
                }
                else
                {
                    out.println("<script>alert('Create flight fail!');javascript:window.history.back(-1);</script>");
                }
            }
        }
        if(request.getParameter("delete")!=null)
        {
            if(request.getParameter("delete").equals("Delete"))
            {
                 int id=Integer.parseInt(request.getParameter("flightID"));
                 if(flbean.DeleteFlight(id)==true)
                 {
                     out.println("<script>alert('Delete flight Success!');window.location='Admin/management.jsp?page=Flights';</script>");
                 }
                 else
                 {
                     out.println("<script>alert('Delete flight fail!');javascript:window.history.back(-1);</script>");
                 }
            }
        }
        if(request.getParameter("edit")!=null)
        {
             if(request.getParameter("edit").equals("Edit"))
             {
                String flightID=request.getParameter("flightID");
                String nameCity=request.getParameter("nameCity");
                String flightName=request.getParameter("flightName");
                String departureTime=request.getParameter("departureTime");
                String arrivalTime=request.getParameter("arrivalTime");
                String departureCity=request.getParameter("departureCity");
                String arrivalCity=request.getParameter("arrivalCity");
                String price=request.getParameter("price");
                
                String timedeparture=departureTime.substring(departureTime.length()-2,departureTime.length());
                String timearrival=arrivalTime.substring(arrivalTime.length()-2,arrivalTime.length());
              
                String departime=departureTime.substring(0,departureTime.length()-3);
                String arrivatime=arrivalTime.substring(0,arrivalTime.length()-3);
                out.println(departime+":"+arrivatime);
                session.setAttribute("flightID",flightID); 
                session.setAttribute("nameCity",nameCity);
                session.setAttribute("flightName",flightName);
                session.setAttribute("departureTime",departime);
                session.setAttribute("timedeparture",timedeparture);
                session.setAttribute("timearrival",timearrival);
                session.setAttribute("arrivalTime",arrivatime);
                session.setAttribute("departureCity",departureCity);
                session.setAttribute("arrivalCity",arrivalCity);
                session.setAttribute("price",price);
                session.setAttribute("status","True");
                
                response.sendRedirect("Admin/management.jsp?page=Flights");
             }
        }
         if(request.getParameter("update")!=null)
        {
             if(request.getParameter("update").equals("Update Flight"))
             {
                int id=Integer.parseInt(request.getParameter("flightID"));
                int lcname=Integer.parseInt(request.getParameter("namecity"));
                String flname=request.getParameter("flightname");
                float flpice=Float.parseFloat(request.getParameter("price"));
                String departuretime =request.getParameter("departurTime");
                String arrivaltime=request.getParameter("arrivalTime");
                String timedeparture=request.getParameter("timedeparture");
                String timearrival=request.getParameter("timearrival");
                 String departureCity=request.getParameter("departureCity");
                String arrivalcity=request.getParameter("arrivalCity");
                if(flbean.UpdateFlight(lcname,flname,departuretime+" "+timedeparture,arrivaltime+" "+timearrival,departureCity,arrivalcity,flpice,id)==true)
                {
                     session.removeAttribute("flightID");
                     session.removeAttribute("flightName");
                     session.removeAttribute("departureTime");
                     session.removeAttribute("arrivalTime");
                     session.removeAttribute("nameCity");
                     session.removeAttribute("departureCity");
                     session.removeAttribute("arrivalCity");
                     session.removeAttribute("price");
                     session.removeAttribute("status");
                    out.println("<script>alert('Upladte flight Success!');window.location='Admin/management.jsp?page=Flights';</script>");
                }
                else
                {
                    out.println("<script>alert('Update flight fail!');javascript:window.history.back(-1);</script>");
                }
             }
         }
         if(request.getParameter("reset")!=null)
        {
             if(request.getParameter("reset").equals("Cancel Update"))
             {
                     session.removeAttribute("flightID");
                     session.removeAttribute("flightName");
                     session.removeAttribute("departureTime");
                     session.removeAttribute("arrivalTime");
                     session.removeAttribute("nameCity");
                     session.removeAttribute("departureCity");
                     session.removeAttribute("arrivalCity");
                     session.removeAttribute("price");
                     session.removeAttribute("status");
                     response.sendRedirect("Admin/management.jsp?page=Flights");
             }
         }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
